package com.sqsy.util;

import com.mapper.UserMapper;
import com.sqsy.javabean.User;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

public class UserMapperTest {
    SqlSession session;
    UserMapper mapper;

    @Before
    public void before() {
        //1、创建SqlSessionFactory对象，也是单例模式的
        SqlSessionFactory factory = MyBatisUtil.getSqlSessionFactory();
        //2、创建SqlSession对象 true 自动提交事务 不写也没事 因为默认在sqlsession关闭时提交事务
        session = factory.openSession(true);
        //3、调用session的方法namespace的名字.id
        mapper = session.getMapper(UserMapper.class);

    }

    @After
    public void after() {
        //5、关闭资源
        session.close();
    }

    @Test
    public void selectAll(){
        for (User user : mapper.selectAll()) {
            System.out.println(user);
        }
    }

    @Test
    public void selectByUser(){
        User u=new User();
        u.setUid(1);
//        u.setUsername("zhangsan");
        for (User user : mapper.selectByUser(u)) {
            System.out.println(user);
        }
    }

    @Test
    public void selectByUserOne(){
        User u=new User();
        //u.setUid(1);
        //u.setUsername("zhangsan");
        //u.setPhone("1111");
        for (User user : mapper.selectByUserOne(u)) {
            System.out.println(user);
        }
    }

    @Test
    public void trimTest(){
        mapper.trimTest();
    }

    @Test
    public void updateByUser(){
        User u=new User();
        u.setUid(1);
//        u.setUsername("zhangsan1");
//        u.setPassword("aaaa");
//        u.setPhone("1111");
        mapper.updateByUser(u);
    }

    @Test
    public void deleteByArray(){
        int[] ids={9,10,11,12,13,14};
        mapper.deleteByArray(ids);
    }

    @Test
    public void insertList(){
        List<User> list=new ArrayList<>();
        list.add(new User("a","a","a"));
        list.add(new User("b","b","b"));
        list.add(new User("c","c","c"));
        mapper.insertList(list);
    }
}
